import com.lgl.dao.IUserDao;
import com.lgl.io.Resources;
import com.lgl.sqlSession.ISqlSession;
import com.lgl.sqlSession.ISqlSessionFactory;
import com.lgl.sqlSession.impl.SqlSessionFactoryBuilder;
import com.lgl.pojo.User;
import org.dom4j.DocumentException;
import org.junit.Before;
import org.junit.Test;

import java.beans.IntrospectionException;
import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

public class IPersistenceTest {

    private IUserDao userDao;
    @Before
    public void testBefore() throws PropertyVetoException, DocumentException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        ISqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        ISqlSession sqlSession = sqlSessionFactory.openSession();
        userDao = sqlSession.getMapper(IUserDao.class);
    }

    @Test
    public void test() throws PropertyVetoException, DocumentException, IllegalAccessException, ClassNotFoundException, IntrospectionException, InstantiationException, SQLException, InvocationTargetException, NoSuchFieldException {
        List<User> users = userDao.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void addUserTest() {
        User user = new User();
        user.setId(4);
        user.setUsername("gxy");
        user.setPassword("111111");
        user.setBirthday("1990-01-01");
        int result = userDao.addUser(user);
        System.out.println("添加sql成功：" + result);
    }

    @Test
    public void updateUserTest() {
        User user = new User();
        user.setId(4);
        user.setUsername("gxy");
        user.setPassword("222222");
        user.setBirthday("1990-01-01");
        int result = userDao.updateUser(user);
        System.out.println("更新sql成功：" + result);
    }

    @Test
    public void deleteUserTest() {
        User user = new User();
        user.setId(4);
        int result = userDao.deleteUser(user);
        System.out.println("删除sql成功：" + result);
    }

}
